﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using umbraco.cms.businesslogic.member;
using System.Web.Security;
namespace Zebra.Umbraco.Marcos.MailList
{
    public partial class MailListInput : LoginRequiredControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnBack.Attributes["onClick"] = string.Format("location.href='/MailList/{0}/detail.aspx';", ListID);

                using (DataClasses1DataContext db = new DataClasses1DataContext())
                {
                    surveyMailList mailList = db.surveyMailLists.First(c => c.MailListID == ListID && c.NodeID == int.Parse(Membership.GetUser().ProviderUserKey.ToString()));
                    if (mailList != null)
                    {
                        lbMailListName.Text = mailList.Name;
                    }
                    //lbMailListName
                }

            }
        }

        public int ListID
        {
            get
            {
                return int.Parse(Request["ListID"]);
            }

        }
        int success = 0;
        int duplicated = 0;
        protected void Button1_Click(object sender, EventArgs e)
        {
            using (DataClasses1DataContext db = new DataClasses1DataContext())
            {
                string errorString = "";
                foreach (string str in TextBox1.Text.Split('\r'))
                {
                    if (str.Split(',').Length == 3 && CheckEmail(str.Split(',')[2]))
                    {
                        AddMailListItem(str.Split(',')[2], str.Split(',')[0], str.Split(',')[1], db);
                    }
                    else
                    {
                        errorString += str + "\r";
                    }
                }
                db.SubmitChanges();

                if (string.IsNullOrEmpty(errorString))
                {
                    Response.Redirect(string.Format("/MailList/{0}/Detail.aspx", ListID));
                }
                else
                {
                    TextBox1.Text = errorString;
                    lbError.Visible = true;
                }
            }
        }

        private bool CheckEmail(string Email)
        {
            Regex mail = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
            return (mail.Match(Email).Success);
        }
        private void AddMailListItem(string Email, string FirstName, string LastName, DataClasses1DataContext db)
        {
            if (db.surveyMailAddresses.Count(c => c.MailListID == ListID && c.LastName == LastName && c.FirstName == FirstName) == 0
                &&
                db.surveyMailAddresses.Count(c => c.MailListID == ListID && c.Address == Email) == 0)
            {
                surveyMailAddress address = new surveyMailAddress()
                {
                    Address = Email,
                    FirstName = FirstName,
                    LastName = LastName,
                    MailListID = ListID
                };
                db.surveyMailAddresses.InsertOnSubmit(address);
                success++;
            }
            else
            {
                duplicated++;
            }
        }
    }
}